import 'package:flutter/material.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:flutterlib/widgets/pull/nested/gif_pull_header.dart';
import 'package:flutterlib/widgets/pull/nested/pull_load_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutterlib/common/utils/utils.dart';


typedef  RenderHeader = List<Widget> Function(BuildContext context, bool innerBoxIsScrolled);
typedef  RenderFooter = List<Widget> Function(BuildContext context, bool innerBoxIsScrolled);
///通用下上刷新控件
class PullLoadSliverWidget extends StatefulWidget {
  ///item渲染
  final IndexedWidgetBuilder itemBuilder;

  final WidgetBuilder emptyBuilder;

  ///加载更多回调
  final RefreshCallback onLoadMore;

  ///下拉刷新回调
  final RefreshCallback onRefresh;

  ///控制器，比如数据和一些配置
  final PullLoadWidgetControl control;

  final RenderHeader headerSliverBuilder;

  final RenderFooter footerSliverBuilder;

  final ScrollController scrollController;

  final RefreshController refreshController;

  final bool enablePullDown;

  final bool enablePullUp;

  PullLoadSliverWidget(
      this.control, this.itemBuilder, this.onRefresh, this.onLoadMore,
      {
      this.headerSliverBuilder,
        this.footerSliverBuilder,
      this.scrollController,
      this.refreshController,
      this.enablePullDown,
      this.enablePullUp, this.emptyBuilder});

  @override
  _PullLoadSliverWidgetState createState() =>
      _PullLoadSliverWidgetState(
        this.control,
        this.itemBuilder,
        this.onRefresh,
        this.onLoadMore,
      );
}

class _PullLoadSliverWidgetState
    extends State<PullLoadSliverWidget> {
  final IndexedWidgetBuilder itemBuilder;

  final RefreshCallback onLoadMore;

  final RefreshCallback onRefresh;


  PullLoadWidgetControl control;

  _PullLoadSliverWidgetState(this.control, this.itemBuilder,
      this.onRefresh, this.onLoadMore);

  @override
  void initState() {
    super.initState();
  }

  _getListCount() {
    ///当数据为空的时候，返回空页面
    if(control.dataList.length == 0){
      return 1;
    }
    return control.dataList.length;
  }

  ///根据配置状态返回实际列表渲染Item
  _getItem(int index) {
    ///回调外部正常渲染Item，如果这里有需要，可以直接返回相对位置的index
    if(control.dataList.length == 0){
      return widget.emptyBuilder != null ? widget.emptyBuilder(context) : _buildEmpty();
    }
    return itemBuilder(context, index);
  }

  void _onOffsetCallback(bool isUp, double offset) {}

  @override
  Widget build(BuildContext context) {
    List<Widget> slivers = [];
    slivers.addAll(widget.headerSliverBuilder(context, true));
    Widget sliverList = SliverList(
        delegate: SliverChildBuilderDelegate(
      (c, i) => _getItem(i),
      childCount: _getListCount(),
    ));
    slivers.add(sliverList);
    slivers.addAll(widget.footerSliverBuilder(context, true));

    return SmartRefresher(
      controller: widget.refreshController,
      enablePullDown: widget.enablePullDown,
      enablePullUp: widget.enablePullUp,
      onRefresh: onRefresh,
      footer: ClassicFooter(
        loadStyle: LoadStyle.ShowWhenLoading,
        noDataText: "到了世界的尽头了...",
        loadingText: "努力加载中，请稍等",
      ),
      header: GifPullHeader(),
      onLoading: () {
        // if (this.control.needLoadMore.value) {
          this.onLoadMore?.call();
        // }
      },
      onOffsetChange: _onOffsetCallback,
      child: CustomScrollView(
        controller: widget.scrollController,
        slivers: slivers,
      ),
    );
  }

  ///空页面
  Widget _buildEmpty() {
    return new Container(
      height: 350.h,
      alignment: Alignment.center,
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          RaisedButton.icon(
            icon: Icon(Icons.refresh),
            label: Text("请刷新"),
            onPressed: () {widget.refreshController?.requestRefresh();},
          ),
          Container(
            child: Text("数据为空", style: QuhuiConstant.normalText),
          ),
        ],
      ),
    );
  }
}

